<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        WebChat.renderWebChat(
          {
            directLine: testHelpers.createDirectLineWithTranscript(
              testHelpers.transcriptNavigation.generateTranscript()
            ),
            store: testHelpers.createStore()
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();
        await pageConditions.numActivitiesShown(32);
        await pageConditions.scrollToBottomCompleted();

        await pageObjects.focusSendBoxTextBox();

        // Tests according to https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_focus_activedescendant.

        // Mouse: Clicking on activity should not scroll activity into view.

        // WHEN: Clicking on top left of the transcript.
        await host.clickAt(1, 1, pageElements.transcriptScrollable());

        // THEN: It should not scroll activity #29 into view.
        await pageConditions.scrollStabilized(
          pageElements.transcriptScrollable().scrollHeight - pageElements.transcriptScrollable().offsetHeight
        );

        // THEN: It should show a visual indicator around activity #29.
        expect(pageElements.activeActivity()).toBe([...pageElements.activities()].reverse()[2]);

        // THEN: It should not scroll activity #29 into the view.
        //       Activity #31 should be bottom aligned and displayed completely.
        await host.snapshot();

        // WHEN: Clicking on the <input> inside activity #29.
        await host.click(pageElements.focusedActivity().querySelector('input'));

        // THEN: It should not scroll activity #29 into view.
        await pageConditions.scrollStabilized(
          pageElements.transcriptScrollable().scrollHeight - pageElements.transcriptScrollable().offsetHeight
        );

        // THEN: It should keep activity #29 as `aria-activedescendant`.
        expect(pageElements.activeActivity()).toBe([...pageElements.activities()].reverse()[2]);

        // THEN: It should no longer have a visual indicator around activity #29.
        //       Activity #31 should be bottom aligned and displayed completely.
        await host.snapshot();

        // WHEN: Clicking on the top left of the transcript.
        await host.clickAt(1, 1, pageElements.transcriptScrollable());

        // THEN: It should not scroll activity #29 into view.
        await pageConditions.scrollStabilized(
          pageElements.transcriptScrollable().scrollHeight - pageElements.transcriptScrollable().offsetHeight
        );

        // THEN: It should show a visual indicator around activity #29.
        expect(pageElements.activeActivity()).toBe([...pageElements.activities()].reverse()[2]);

        // THEN: It should show activity #29 partially with top part off the screen.
        //       Activity #31 should be bottom aligned and displayed completely.
        await host.snapshot();
      });
    </script>
  </body>
</html>
